package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.servise.EmpServise;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpServise empServise;
//    @RequestMapping
//    public Result list(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize){
//        log.info("页码"+page+",每页显示数据"+pageSize);
//        PageBean page1 = empServise.page(page, pageSize);
//        return Result.success(page1);
//
//    }
    //员工列表查询
    @GetMapping
    public Result list(EmpQueryParam param){
        log.info("条件参数"+param);
        PageBean page = empServise.page(param);
        return Result.success(page);
    }
    //批量删除员工
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Integer> ids){
        log.info("批量删除员工"+ids);
        empServise.delete(ids);
        return Result.success();
    }

    //新增员工
    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工{}", emp);
        empServise.add(emp);
        return Result.success();
    }
    //查询所有员工
    @GetMapping("/list")
    public Result getAll(){
        return Result.success(empServise.getAll());
    }
    // 根据id查询员工
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询员工{}", id);
        Emp byId = empServise.getById(id);
        return Result.success(byId);
    }
    //  修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工{}", emp);
        log.info("修改员工", emp.getImage());
        empServise.putEmp(emp);
        return Result.success();
    }


}
